ergokv
DISCLAIMER:
THIS IS ALPHA AS FUCK. (not yet suitable for production)
A Rust library for easy integration with TiKV, providing derive macros for automatic CRUD operations.
Installation
Add this to your Cargo.toml
:
[]
= "0.1.8"
Documentation
For detailed documentation, including usage examples and API reference, please visit:
You can also generate the documentation locally by running:
Prerequisites
- Rust (edition 2021 or later)
- Protobuf
- GRPC
- TiKV (can be installed via TiUP or automatically via LocalCluster)
Installing TiKV
There are two primary ways to install TiKV:
-
Manual Installation with TiUP:
# Install TiUP (TiKV's package manager) | # Set up TiKV cluster for development
-
Automatic Installation with LocalCluster:
use LocalCluster; // LocalCluster automatically downloads and sets up TiKV if not present let cluster = start.unwrap; let client = cluster.spawn_client.await.unwrap;
LocalCluster is particularly useful for development and testing, as it automatically handles TiKV installation and cluster setup.
Attributes
The Store
derive supports several attributes to customize your data
model:
-
@[key]
: Marks the primary key field (exactly one field must have this attribute) -
@[unique_index]
: Creates a unique index on a field, allowing efficient lookup with guaranteed uniqueness -
@[index]
: Creates a non-unique index on a field, allowing multiple entities to share the same indexed value -
@[migrate_from]
: Used for schema migrations, specifying the previous version of the struct -
@[model_name]
: Used during migrations when the struct name changes// Helps track model across versions
Usage
Basic usage with various index types:
use Store;
use ;
use Uuid;
async
Longer example:
use Store;
use ;
use Uuid;
async
Backup and Restore
The Store
derive automatically implements backup and restore
functionality for your models:
use Store;
use ;
use Uuid;
async
Backups are stored as line-delimited JSON files, with automatic
timestamping: User_1708644444.json
. Each line contains one serialized
instance, making the backups human-readable and easy to process with
standard tools.
Migrations
Store migrations are supported via the `#[migratefrom]` attribute. This allows you to evolve your data structures while keeping data integrity.
Example
The recommended approach is to use private submodules for versioning models and always re-export the latest version:
Note: The `#[modelname]` attribute is required when the struct name changes between versions (like UserV1 -> User above). This ensures ergokv can track the underlying model correctly across migrations.
Run migrations:
.await?;
ensure_migrations
Running TiKV
For Development
Use TiUP playground:
This sets up a local TiKV cluster for testing.
For Production
-
Create a topology file (e.g., `topology.yaml`):
global: user: "tidb" ssh_port: 22 deploy_dir: "/tidb-deploy" data_dir: "/tidb-data" pd_servers: - host: 10.0.1.1 - host: 10.0.1.2 - host: 10.0.1.3 tikv_servers: - host: 10.0.1.4 - host: 10.0.1.5 - host: 10.0.1.6 tidb_servers: - host: 10.0.1.7 - host: 10.0.1.8 - host: 10.0.1.9
-
Deploy the cluster:
-
Start the cluster:
Testing
To run tests, ensure you have TiUP installed and then use:
Tests will automatically start and stop a TiKV instance using TiUP.
I will be honest with you, chief, I made one test and that's it.
License
This project is licensed under the Fair License:
Copyright (c) 2024 Lukáš Hozda
Usage of the works is permitted provided that this instrument is retained with the works, so that any entity that uses the works is notified of this instrument.
DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
There is a lot of things that could be improved:
- Make ergokv support more KV stores
- Improve documentation
- Allow swapping the serialization format (currently we use CBOR via ciborium)
- Let methods be generic (in the case of TiKV) over RawClient, Transaction and TransactionClient
- Add additional methods that retrieve multiple structures, to make it useful to e.g. fetch entities like articles and all users (note that this can be done already by manually making a sort of entity registry for yourself)